package com.molichuxing.gateway.bff.car.convertor;

import com.molichuxing.framework.dto.Paged;
import com.molichuxing.framework.utils.DateUtil;
import com.molichuxing.gateway.bff.car.entity.request.modify.CheckModifyVo;
import com.molichuxing.gateway.bff.car.entity.request.modify.DisPdiCheckModifyVo;
import com.molichuxing.gateway.bff.car.entity.request.modify.PdiCheckModifyVo;
import com.molichuxing.gateway.bff.car.entity.response.*;
import com.molichuxing.services.business.dto.response.*;
import com.molichuxing.services.infrastructure.dto.request.modify.CheckModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.DisPdiCheckModifyDto;
import com.molichuxing.services.infrastructure.dto.request.modify.PdiCheckModifyDto;
import com.molichuxing.services.infrastructure.dto.response.*;
import com.molichuxing.services.infrastructure.dto.response.CarDto;
import com.molichuxing.services.property.CarCheckStatusEnum;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Convert {

    /********************************************车辆事故信息转换 开始 *****************************/

    public static Paged<AccidentBizVo> toCarAccidentVoPage(Paged<AccidentBizDto> page, Map<Integer, UserDto> userMap) {
        Paged<AccidentBizVo> result = null;;
        if(null != page) {
            result = new Paged<>();
            BeanUtils.copyProperties(page, result);
            result.setList(toCarAccidentVoPage(page.getList(), userMap));
        }
        return result;
    }

    public static List<AccidentBizVo> toCarAccidentVoPage(List<AccidentBizDto> beanList, Map<Integer, UserDto> userMap) {
        List<AccidentBizVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (AccidentBizDto bean : beanList) {
                dtoList.add(toCarAccidentVoPage(bean, userMap));
            }
        }
        return dtoList;
    }

    public static AccidentBizVo toCarAccidentVoPage(AccidentBizDto bean, Map<Integer, UserDto> userMap) {
        AccidentBizVo beanDto = null;
        if (bean != null) {
            beanDto = new AccidentBizVo();
            BeanUtils.copyProperties(bean, beanDto);
            if((null != userMap)&&(null != userMap.get(bean.getUserId()))) {
                beanDto.setUserName(userMap.get(bean.getUserId()).getRealName());
            }
        }
        return beanDto;
    }
    public static AccidentDetailVo toAccidentDetailVo(AccidentDetailDto bean) {
        AccidentDetailVo beanDto = null;
        if (bean != null) {
            beanDto = new AccidentDetailVo();
            BeanUtils.copyProperties(bean, beanDto);
            AccidentCarUserVo accidentCarUserVo = new AccidentCarUserVo();
            BeanUtils.copyProperties(bean.getAccidentCarUserDto(), accidentCarUserVo);
            beanDto.setAccidentCarUserVo(accidentCarUserVo);

            beanDto.setStatus(toAccidentStatusVo(bean.getStatus()));

            AccidentInfoVo accidentInfoVo = new AccidentInfoVo();
            BeanUtils.copyProperties(bean.getAccidentInfoDto(), accidentInfoVo);
            if(null != bean.getAccidentInfoDto().getAccidentTime()) {
                accidentInfoVo.setAccidentTime(DateUtil.formatTime(bean.getAccidentInfoDto().getAccidentTime()));
            }
            if(null != bean.getAccidentInfoDto().getReportTime()) {
                accidentInfoVo.setReportTime(DateUtil.formatTime(bean.getAccidentInfoDto().getReportTime()));
            }
            if(null != bean.getAccidentInfoDto().getCaseTime()) {
                accidentInfoVo.setCaseTime(DateUtil.formatTime(bean.getAccidentInfoDto().getCaseTime()));
            }
            if(null != bean.getAccidentInfoDto().getIsDie()) {
                accidentInfoVo.setIsDie(bean.getAccidentInfoDto().getIsDie());
            }
            if(null != bean.getAccidentInfoDto().getIsDieStr()) {
                accidentInfoVo.setIsDieStr(bean.getAccidentInfoDto().getIsDieStr());
            }
            beanDto.setAccidentInfoVo(accidentInfoVo);

            AccidentCommitVo commit = new AccidentCommitVo();
            BeanUtils.copyProperties(bean.getCommit(), commit);
            beanDto.setCommit(commit);
        }
        return beanDto;
    }

    public static Map<Integer, AccidentStatusVo> toAccidentStatusVo(Map<Integer, AccidentStatusDto> map) {
        Map<Integer, AccidentStatusVo> result = null;
        if (map != null) {
            result = new HashMap<>();
            for (Integer key : map.keySet()) {
                result.put(key, toAccidentStatusVo(map.get(key)));
            }
        }
        return result;
    }

    public static AccidentStatusVo toAccidentStatusVo(AccidentStatusDto bean) {
        AccidentStatusVo beanDto = null;
        if (bean != null) {
            beanDto = new AccidentStatusVo();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }


    /********************************************车辆事故信息转换 开始 *****************************/


    /********************************************车辆信息转换 开始 *****************************/

    public static Paged<CarVo> toCarVoPage(Paged<CarDto> page, Map<Integer, String> vehicleMap, Map<Integer, VehicleColorDto> carDtoMap) {
        Paged<CarVo> result = null;;
        if(null != page) {
            result = new Paged<>();
            BeanUtils.copyProperties(page, result);
            result.setList(toCarVoPage(page.getList(), vehicleMap, carDtoMap));
        }
        return result;
    }

    public static List<CarVo> toCarVoPage(List<CarDto> beanList, Map<Integer, String> vehicleMap, Map<Integer, VehicleColorDto> carDtoMap) {
        List<CarVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (CarDto bean : beanList) {
                dtoList.add(toCarVoPage(bean,vehicleMap, carDtoMap));
            }
        }
        return dtoList;
    }

    public static CarVo toCarVoPage(CarDto bean, Map<Integer, String> vehicleMap, Map<Integer, VehicleColorDto> carDtoMap) {
        CarVo beanDto = null;
        if (bean != null) {
            beanDto = new CarVo();
            BeanUtils.copyProperties(bean, beanDto);
            if((null != vehicleMap)&&(null != vehicleMap.get(bean.getVehicleId()))) {
                beanDto.setVehicleName(vehicleMap.get(bean.getVehicleId()));
            }
            if((null != carDtoMap)&&(null != carDtoMap.get(bean.getVehicleColorId()))) {
                beanDto.setVehicleColorName(carDtoMap.get(bean.getVehicleColorId()).getName());
            }
        }
        return beanDto;
    }

    public static CarDetailVo toCarDetailVo(CarDetailBizDto bean) {
        CarDetailVo beanDto = null;
        if (bean != null) {
            beanDto = new CarDetailVo();
            BeanUtils.copyProperties(bean, beanDto);
            List<String> list = new ArrayList<>();
            if(null != bean.getFactoryCertImg()){
                list.add(bean.getFactoryCertImg());
            }
            if(null != bean.getPurchaseInvoiceImg()){
                list.add(bean.getPurchaseInvoiceImg());
            }
            if(null != bean.getUniformityCertImg()){
                list.add(bean.getUniformityCertImg());
            }

            if(null != bean.getRegisterCard()){
                list.add(bean.getRegisterCard());
            }
            if(null != bean.getDrivingLicense()){
                list.add(bean.getDrivingLicense());
            }
            if(null != bean.getTaxProof()){
                list.add(bean.getTaxProof());
            }
            beanDto.setImgList(list);
        }
        return beanDto;
    }

    public static List<CarInsuranceVo> toCarInsuranceVoPage(List<CarInsuranceDto> beanList) {
        List<CarInsuranceVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (CarInsuranceDto bean : beanList) {
                dtoList.add(toCarInsuranceVoPage(bean));
            }
        }
        return dtoList;
    }


    public static CarInsuranceVo toCarInsuranceVoPage(CarInsuranceDto bean) {
        CarInsuranceVo beanDto = null;
        if (bean != null) {
            beanDto = new CarInsuranceVo();
            BeanUtils.copyProperties(bean, beanDto);
            beanDto.setBeginTime(DateUtil.formatDate(bean.getBeginTime()));
            beanDto.setEndTime(DateUtil.formatDate(bean.getEndTime()));
            beanDto.setInsureTime(DateUtil.formatDate(bean.getInsureTime()));
        }
        return beanDto;
    }

    public static List<CarsViolationVo> toCarsViolationVo(List<CarsViolationDto> beanList) {
        List<CarsViolationVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (CarsViolationDto bean : beanList) {
                dtoList.add(toCarsViolationVo(bean));
            }
        }
        return dtoList;
    }

    public static CarsViolationVo toCarsViolationVo(CarsViolationDto bean) {
        CarsViolationVo beanDto = null;
        if (bean != null) {
            beanDto = new CarsViolationVo();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }


    public static List<CarsViolationVo> toCarsViolationVo(List<CarsViolationDto> beanList, Map<Integer, UserBizDto> userMap) {
        List<CarsViolationVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (CarsViolationDto bean : beanList) {
                dtoList.add(toCarsViolationVo(bean, userMap));
            }
        }
        return dtoList;
    }

    public static CarsViolationVo toCarsViolationVo(CarsViolationDto bean, Map<Integer, UserBizDto> userMap) {
        CarsViolationVo beanDto = null;
        if (bean != null) {
            beanDto = new CarsViolationVo();
            BeanUtils.copyProperties(bean, beanDto);
            if((null != userMap)&&(null != userMap.get(bean.getUserId()))) {
                beanDto.setName(userMap.get(bean.getUserId()).getName());
                beanDto.setRealName(userMap.get(bean.getUserId()).getRealName());
                beanDto.setPhone(userMap.get(bean.getUserId()).getPhone());
                beanDto.setIdNumber(userMap.get(bean.getUserId()).getIdNumber());
            }
        }
        return beanDto;
    }


    //车辆检验图片LIST
    public static List<CarCheckVo> toCarCheckVoList(List<CarCheckDto> beanList) {
        List<CarCheckVo> dtoList = null;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (CarCheckDto bean : beanList) {
                dtoList.add(toCarCheckVo(bean));
            }
        }
        return dtoList;
    }



    /********************************************车辆信息转换 结束*****************************/
    /********************************************车辆检验转换 开始 *****************************/

    //车辆检验
    public static CarCheckVo toCarCheckVo(CarCheckDto bean) {
        CarCheckVo beanDto = null;
        if (bean != null) {
            beanDto = new CarCheckVo();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }

    //车辆检验
    public static Paged<CarCheckVo> toPagedCarCheckVo(Paged<CarCheckDto> bean) {
        Paged<CarCheckVo> beanDto = null;
        if (bean != null) {
            beanDto = new Paged<CarCheckVo>();
            BeanUtils.copyProperties(bean, beanDto);
            beanDto.setList(toCarCheckVoList(bean.getList()));
        }
        return beanDto;
    }


    //车辆检验信息
    public static CheckVo toCheckVo(CheckDto bean) {
        CheckVo beanDto = null;
        if (bean != null) {
            beanDto = new CheckVo();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }


    public static PdiCheckVo toPdiCheckVo(CheckDto bean, CheckDisputeDto checkDisputeDto) {
        PdiCheckVo beanDto = null;
        if (bean != null) {
            beanDto = new PdiCheckVo();
            BeanUtils.copyProperties(bean, beanDto);
        }
        if (checkDisputeDto != null) {
            if (bean.getStatus() == CarCheckStatusEnum.ALREADY_CHECK.getValue()) {
                beanDto.setIsUserApproves(true);
                beanDto.setIsUserApprovesStr("是");
            } else {
                beanDto.setIsUserApproves(checkDisputeDto.getIsUserApproves());
                beanDto.setIsUserApprovesStr((checkDisputeDto.getIsUserApproves() != null && checkDisputeDto.getIsUserApproves()) ? "是" :"否");
                if (!checkDisputeDto.getIsUserApproves()) {
                    beanDto.setReason(checkDisputeDto.getReason());
                }
            }
            beanDto.setDiscussLossFee(checkDisputeDto.getDiscussLossFee());
            beanDto.setDiscussremark(checkDisputeDto.getRemark());
            beanDto.setDiscussLossOpName(checkDisputeDto.getOpName()); //检验人
            beanDto.setDiscussLossCheckTime(checkDisputeDto.getCheckTime()); //检验时间
            if (checkDisputeDto.getIsUserApproves() || checkDisputeDto.getDiscussLossFee() == null) {
                beanDto.setHaveDiscussLoss(false);
            }
        } else {
            beanDto.setHaveDiscussLoss(false);
        }
        return beanDto;
    }

    public static CheckModifyDto toCheckModifyDto(CheckModifyVo bean) {
        CheckModifyDto beanDto = null;
        if (bean != null) {
            beanDto = new CheckModifyDto();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }


    public static PdiCheckModifyDto toPdiCheckModifyDto(PdiCheckModifyVo bean) {
        PdiCheckModifyDto beanDto = null;
        if (bean != null) {
            beanDto = new PdiCheckModifyDto();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }

    public static DisPdiCheckModifyDto toDisPdiCheckModifyDto(DisPdiCheckModifyVo bean) {
        DisPdiCheckModifyDto beanDto = null;
        if (bean != null) {
            beanDto = new DisPdiCheckModifyDto();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }


    public static List<MaintenanceRecordVo> toMaintenanceRecordVo(List<MaintenanceRecordBizDto> beanList) {
        List<MaintenanceRecordVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (MaintenanceRecordBizDto bean : beanList) {
                dtoList.add(toMaintenanceRecordVo(bean));
            }
        }

        return dtoList;
    }

    public static MaintenanceRecordVo toMaintenanceRecordVo(MaintenanceRecordBizDto bean) {
        MaintenanceRecordVo beanDto = null;
        if (bean != null) {
            beanDto = new MaintenanceRecordVo();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }

    public static List<IovTrajectoryVo> toIovTrajectoryVo(List<IovTrajectoryDto> beanList) {
        List<IovTrajectoryVo> dtoList = null;;
        if (beanList != null && beanList.size() > 0) {
            dtoList = new ArrayList<>();
            for (IovTrajectoryDto bean : beanList) {
                dtoList.add(toIovTrajectoryVo(bean));
            }
        }
        return dtoList;
    }

    public static IovTrajectoryVo toIovTrajectoryVo(IovTrajectoryDto bean) {
        IovTrajectoryVo beanDto = null;
        if (bean != null) {
            beanDto = new IovTrajectoryVo();
            BeanUtils.copyProperties(bean, beanDto);
        }
        return beanDto;
    }

    /********************************************车辆检验 结束 *****************************/


}
